
/**
 * name:        个人中心--售后服务步骤一
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){
    // 个人中心首页
    // var URL = url+'/'; 

    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token='' 
        isLogin();    
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            var orderId =GetQueryString("orderId"); 
            var goodsId =GetQueryString("goodsId");  
            getApplyOrderInfo(token,URL,orderId,goodsId);
            //getOrderDetail(token,URL,orderId);
            
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){
                    tc(data.message)
                }  
            })
            setTimeout(function(){
                window.location.href='/index.html'
            },1100);
    });

    

});

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}

function getApplyOrderInfo(token,URL,orderId,goodsId){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_refund/getApplyOrderInfo',
        dataType:'json',
        data:{token:token,orderId:orderId,goodsId:goodsId},
        success:function(e){ 
            var serviceGoodsTpl = $("#serviceGoodsTpl").html();
            var time = e.data.orderInfo.createTime;
            var maxPrice = e.data.maxRefundAmount;
            var maxNum = e.data.maxRefundNum;
            function getLocalTime(nS) {     
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
            }
            if(e.data.orderInfo.goodsSpec==''||e.data.orderInfo.goodsSpec==null){
                e.data.orderInfo.goodsSpec = '默认';
            }
            $("#maxRefundAmount").text(maxPrice);     // 最多可退金额
            $("#refundPrice").val(maxPrice);
            $("#maxRefundNum").text(maxNum);           // 最多可退数量
            $("#refundNum").val(maxNum)
            var serviceGoodsCont=template(
                serviceGoodsTpl, {
                    serviceGoodsList: [{
                        orderId:e.data.orderInfo.orderId,
                        orderNo:e.data.orderInfo.orderNo,
                        createTime:getLocalTime(time),
                        payNumber:e.data.orderInfo.payNumber,
                        goodsId:e.data.orderInfo.goodsId,
                        goodsName:e.data.orderInfo.name,
                        goodsSpec:e.data.orderInfo.spec,
                        payPrice:e.data.orderInfo.payPrice,
                        unitPayPrice:e.data.orderInfo.unitPayPrice,
                        goodsImg:e.data.orderInfo.image,
                        buyNum:e.data.orderInfo.buyNum,
                    }]
                }
            );
            $('.service_goods').append(serviceGoodsCont);
            $.ajax({
                type:'post',
                url:new_url+'shop/member_order/getOrderDetail',
                dataType:'json',
                data:{token:token,orderId:orderId},
                success:function(t){ 
                    if(t.code == '0'){ 
                        $('.service_view_detail .order_status').text(t.data.orderInfo.stateDesc);   
                        $('.service_view_detail .order_detail').text('订单详情');
                    }else{
                        tc(t.message);
                    }
                }  
            });
            $("#serviceOne").click(function(){
                if($("#serviceOne:checked")){
                    $("#refundNum").attr("readonly","readonly");  
                }
            });
            $("#serviceTwo").click(function(){
                if($("#serviceTwo:checked")){
                    $("#refundNum").removeAttr('readonly')
                }
            });
            $("#refundPrice").blur(function(){
                if($(this).val() >= maxPrice){
                    $(this).val(maxPrice);
                }
                if($(this).val()<=0){
                    $(this).val(0.01);
                }
            });
            $("#refundNum").blur(function(){
                if($(this).val() >= maxNum){
                    $(this).val(maxNum);
                }
                if($(this).val()<=0){
                    $(this).val(1);
                }
            });
            // 提交
            $(".service_form_btn").click(function(){
                var refundType = '';
                var refundPrice = $("#refundPrice").val();
                var refundNum = $("#refundNum").val()
                if($('#serviceOne').is(':checked')){ 
                    refundType = 1;
                }
                if($('#serviceTwo').is(':checked')){ 
                    refundType = 2;
                }
                var reason = $("#reason").find("option:selected").text();
                var description ='';
                if($("#description").val()=='' || $("#description").val()==null){
                    tc('请输入问题描述');
                }else{
                    description = $("#description").val();
                    var data = {
                        token:token,
                        orderId:orderId,
                        goodsId:goodsId,
                        refundType:refundType,
                        refundPrice:refundPrice,
                        refundNum:refundNum,
                        reason:reason,
                        description:description
                    }
                    $.ajax({
                        type:'post',
                        url:new_url+'shop/member_refund/applyForRefund',
                        dataType:'json',
                        data:data,
                        success:function(s){
                            tc(s.message)
                            setTimeout(function(){
                                window.location.href='/src/member/member_service_index.html?pageNo=1'
                            },1100)
                        }  
                    });
                }
                
            });

        }
    })
}
